#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#

from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU
from base.Sequence import Sequence

# *********************************************************************************
# generate N misaligned random RVC load/stores...
# *********************************************************************************

# Riscv RVC load/store instrs, word/dword accesses:
RVC_load_store_instructions = {
    "C.FLD##CALM": 10,
    "C.LD##CALM": 10,
    "C.LW##CALM": 10,
    "C.FLDSP##CALM": 10,
    "C.LDSP##CALM": 10,
    "C.LWSP##CALM": 10,
    "C.FSD##CALM": 10,
    "C.SD##CALM": 10,
    "C.SW##CALM": 10,
    "C.FSDSP##CALM": 10,
    "C.SDSP##CALM": 10,
    "C.SWSP##CALM": 10,
}

RVC32_load_store_instructions = {
    "C.FLD##CALM": 10,
    "C.LW##CALM": 10,
    "C.FLDSP##CALM": 10,
    "C.LWSP##CALM": 10,
    "C.FSD##CALM": 10,
    "C.SW##CALM": 10,
    "C.FSDSP##CALM": 10,
    "C.SWSP##CALM": 10,
}


class MyMainSequence(Sequence):
    def generate(self, **kargs):
        for _ in range(100):
            # pick random RVC load/store instruction...
            if self.getGlobalState("AppRegisterWidth") == 32:
                instr = self.pickWeighted(RVC32_load_store_instructions)
            else:
                instr = self.pickWeighted(RVC_load_store_instructions)
            # pick a random address aligned to a page boundary,
            # then (re)align that address close to the end of the page, on
            # half-word boundary. should yield a fair amount of misaligned
            # load/stores...
            target_addr = self.genVA(Align=0x1000) | 0xFFE
            self.notice(">>>>>  Instruction: {} Target addr: {:012x}".format(instr, target_addr))
            self.genInstruction(instr, {"LSTarget": target_addr})


MainSequenceClass = MyMainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
